#include "func_pin_image.h"
// 调整布局参数
#define PIN_ITEM_WIDTH  40
#define PIN_ITEM_HEIGHT 40 // 减小高度
#define LED_SIZE        16
#define ROW_SPACING     0 // 减小行间距




/*
* @brief 引脚状态显示区域
* @param parent 父对象
* @param row 行数
* @param col 列数
* @param pin_names 引脚名称数组
*/
void func_pin_image(lv_obj_t *parent, uint8_t row, uint8_t col, pin_item_t *pin_item_msg)
{
    /******************** 引脚状态显示区域********************/

    lv_obj_t *pin_container = lv_obj_create(parent);
    lv_obj_remove_style_all(pin_container);
    lv_obj_set_size(pin_container, lv_pct(100), lv_pct(100)); // 填充剩余高度
    lv_obj_set_style_bg_color(pin_container, lv_color_hex(0x1A1A1A), LV_PART_MAIN);

    lv_obj_set_style_pad_row(pin_container, ROW_SPACING, LV_PART_MAIN); // 最小行间距
    lv_obj_set_flex_flow(pin_container, LV_FLEX_FLOW_COLUMN);
    lv_obj_clear_flag(pin_container, LV_OBJ_FLAG_SCROLLABLE);

    // 创建紧凑的两排容器
    lv_obj_t *top_row = lv_obj_create(pin_container);
    lv_obj_remove_style_all(top_row);
    lv_obj_set_size(top_row, lv_pct(100), PIN_ITEM_HEIGHT);
    lv_obj_set_style_pad_all(top_row, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_column(top_row, 0, LV_PART_MAIN); // 移除列间距
    lv_obj_set_flex_flow(top_row, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(top_row, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    lv_obj_t *bottom_row = lv_obj_create(pin_container);
    lv_obj_remove_style_all(bottom_row);
    lv_obj_set_size(bottom_row, lv_pct(100), PIN_ITEM_HEIGHT);
    lv_obj_set_style_pad_all(bottom_row, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_column(bottom_row, 0, LV_PART_MAIN); // 移除列间距
    lv_obj_set_flex_flow(bottom_row, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(bottom_row, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    
    int i,j;
    for ( i= 0; i < row; i++) {
        if(i%2)//单数列
        {
            // 创建下排8个引脚 (8-15)
            for ( j = i * col; j < (i+1) * col; j++) {
                // 创建单个引脚容器
                lv_obj_t *pin_item = lv_obj_create(bottom_row);
                lv_obj_remove_style_all(pin_item);
                lv_obj_set_size(pin_item, PIN_ITEM_WIDTH, PIN_ITEM_HEIGHT);
                lv_obj_set_flex_flow(pin_item, LV_FLEX_FLOW_COLUMN); // 垂直布局
                lv_obj_set_flex_align(pin_item, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
                lv_obj_clear_flag(pin_item, LV_OBJ_FLAG_SCROLLABLE); // 禁用滚动
                // LED指示灯
                lv_obj_t *led = lv_led_create(pin_item);
                if (led) {
                    lv_led_set_color(led, lv_color_hex(pin_item_msg->color[j]));

                    lv_led_set_brightness(led, (pin_item_msg->status[j]) ? LV_LED_BRIGHT_MAX : LV_LED_BRIGHT_MIN);
                    lv_obj_set_size(led, LED_SIZE, LED_SIZE);
                    lv_obj_set_style_radius(led, LV_RADIUS_CIRCLE, LV_PART_MAIN);
                }
                // 引脚编号标签
                lv_obj_t *pin_label = lv_label_create(pin_item);
                if (pin_label) {
                    lv_label_set_text(pin_label, pin_item_msg->name[j]);
                    lv_obj_set_style_text_color(pin_label, lv_color_white(), LV_PART_MAIN);
                    lv_obj_set_style_text_font(pin_label, &lv_font_montserrat_14, LV_PART_MAIN);
                }
            }
        }else{
            // 创建上排8个引脚 (0-7)
            for ( j = i * col; j < (i + 1) * col; j++) {
                // 创建单个引脚容器
                lv_obj_t *pin_item = lv_obj_create(top_row);
                lv_obj_remove_style_all(pin_item);
                lv_obj_set_size(pin_item, PIN_ITEM_WIDTH, PIN_ITEM_HEIGHT);
                lv_obj_set_flex_flow(pin_item, LV_FLEX_FLOW_COLUMN); // 垂直布局
                lv_obj_set_flex_align(pin_item, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
                lv_obj_clear_flag(pin_item, LV_OBJ_FLAG_SCROLLABLE); // 禁用滚动

                // 引脚编号标签
                lv_obj_t *pin_label = lv_label_create(pin_item);
                if (pin_label) {
                    lv_label_set_text(pin_label, pin_item_msg->name[j]);
                    lv_obj_set_style_text_color(pin_label, lv_color_white(), LV_PART_MAIN);
                    lv_obj_set_style_text_font(pin_label, &lv_font_montserrat_14, LV_PART_MAIN);
                }

                // LED指示灯
                lv_obj_t *led = lv_led_create(pin_item);
                if (led) {
                    lv_led_set_color(led, lv_color_hex(pin_item_msg->color[j]));

                    lv_led_set_brightness(led,(pin_item_msg->status[j]) ?LV_LED_BRIGHT_MAX:LV_LED_BRIGHT_MIN);
                    lv_obj_set_size(led, LED_SIZE, LED_SIZE);
                    lv_obj_set_style_radius(led, LV_RADIUS_CIRCLE, LV_PART_MAIN);
                }
            }
        }
    }
}
lv_obj_t **find_status_led(lv_obj_t *root, uint16_t *count)
{
    if (!root) {
        *count = 0;
        return NULL;
    }

    // 获取 pin_container (root 的第一个子对象)
    lv_obj_t *pin_container = lv_obj_get_child(root, 0);
    if (!pin_container) {
        *count = 0;
        return NULL;
    }

    // 获取 top_row (pin_container 的第一个子对象)
    lv_obj_t *top_row = lv_obj_get_child(pin_container, 0);
    if (!top_row) {
        *count = 0;
        return NULL;
    }

    // 获取 bottom_row (pin_container 的第二个子对象)
    lv_obj_t *bottom_row = lv_obj_get_child(pin_container, 1);
    if (!bottom_row) {
        *count = 0;
        return NULL;
    }

    // 计算总引脚数
    uint16_t top_count = lv_obj_get_child_cnt(top_row);
    uint16_t bottom_count = lv_obj_get_child_cnt(bottom_row);
    uint16_t total_pins = top_count + bottom_count;

    // 分配内存存储LED指针数组
    lv_obj_t **leds = lv_mem_alloc(sizeof(lv_obj_t *) * total_pins);
    if (!leds) {
        *count = 0;
        return NULL;
    }

    *count = total_pins;
    uint16_t index = 0;

    // 处理上排引脚 (标签在LED上方)
    for (uint16_t i = 0; i < top_count; i++) {
        lv_obj_t *pin_item = lv_obj_get_child(top_row, i);
        if (!pin_item)
            continue;

        // 上排引脚布局: [0]标签 [1]LED
        lv_obj_t *led = lv_obj_get_child(pin_item, 1);
        if (led) {
            leds[index++] = led;
        }
    }

    // 处理下排引脚 (LED在标签上方)
    for (uint16_t i = 0; i < bottom_count; i++) {
        lv_obj_t *pin_item = lv_obj_get_child(bottom_row, i);
        if (!pin_item)
            continue;

        // 下排引脚布局: [0]LED [1]标签
        lv_obj_t *led = lv_obj_get_child(pin_item, 0);
        if (led) {
            leds[index++] = led;
        }
    }

    // 确保实际找到的数量匹配
    *count = index;
    return leds;
}